home *** CD-ROM | disk | FTP | other *** search
/ 17 Bit Software 6: Level 6 / 17 Bit - Level 6 (1998)(Epic Marketing)[!].iso / quartz / q0709.dms / q0709.adf / Graphics / LowLevelGraphics / Example5.c < prev    next >
C/C++ Source or Header  |  1992-07-29  |  7KB  |  217 lines

  1. /***********************************************************/
  2. /*                                                         */
  3. /* Amiga C Encyclopedia (ACE) V3.0      Amiga C Club (ACC) */
  4. /* -------------------------------      ------------------ */
  5. /*                                                         */
  6. /* Book:    ACM Graphics                Amiga C Club       */
  7. /* Chapter: LowLevelGraphics            Tulevagen 22       */
  8. /* File:    Example5.c                  181 41  LIDINGO    */
  9. /* Author:  Anders Bjerin               SWEDEN             */
  10. /* Date:    92-04-28                                       */
  11. /* Version: 1.00                                           */
  12. /*                                                         */
  13. /*   Copyright 1992, Anders Bjerin - Amiga C Club (ACC)    */
  14. /*                                                         */
  15. /* Registered members may use this program freely in their */
  16. /*     own commercial/noncommercial programs/articles.     */
  17. /*                                                         */
  18. /***********************************************************/
  19.  
  20. /* This example demonstrates how to open a ViewPort in interlace mode. */
  21.  
  22.  
  23. #include <intuition/intuition.h>
  24. #include <graphics/gfxbase.h>
  25.  
  26.  
  27. #define WIDTH  640 /* 640 pixels wide (high resolution)                */
  28. #define HEIGHT 400 /* 400 lines high (interlaced NTSC display)         */ 
  29. #define DEPTH    3 /* 3 BitPlanes should be used, gives eight colours. */
  30. #define COLOURS  8 /* 2^3 = 8                                          */
  31.  
  32.  
  33. struct IntuitionBase *IntuitionBase;
  34. struct GfxBase *GfxBase;
  35.  
  36.  
  37. struct View my_view;
  38. struct View *my_old_view;
  39. struct ViewPort my_view_port;
  40. struct RasInfo my_ras_info;
  41. struct BitMap my_bit_map;
  42. struct RastPort my_rast_port;
  43.  
  44.  
  45. UWORD my_color_table[] =
  46. {
  47.   0x000, /* Colour 0, Black       */
  48.   0x800, /* Colour 1, Red         */
  49.   0xF00, /* Colour 2, Light red   */
  50.   0x080, /* Colour 3, Green       */
  51.   0x0F0, /* Colour 4, Light green */
  52.   0x008, /* Colour 5, Blue        */
  53.   0x00F, /* Colour 6, Light Blue  */
  54.   0xFFF, /* Colour 7, White       */
  55. };
  56.  
  57.  
  58. void clean_up();
  59. void main();
  60.  
  61.  
  62. void main()
  63. {
  64.   UWORD *pointer;
  65.   int loop;
  66.   
  67.  
  68.   /* Open the Intuition library: */
  69.   IntuitionBase = (struct IntuitionBase *)
  70.     OpenLibrary( "intuition.library", 0 );
  71.   if( !IntuitionBase )
  72.     clean_up( "Could NOT open the Intuition library!" );
  73.  
  74.   /* Open the Graphics library: */
  75.   GfxBase = (struct GfxBase *)
  76.     OpenLibrary( "graphics.library", 0 );
  77.   if( !GfxBase )
  78.     clean_up( "Could NOT open the Graphics library!" );
  79.  
  80.  
  81.   /* Save the current View, so we can restore it later: */
  82.   my_old_view = GfxBase->ActiView;
  83.  
  84.  
  85.   /* 1. Prepare the View structure, and give it a pointer to */
  86.   /*    the first ViewPort:                                  */
  87.   InitView( &my_view );
  88.   my_view.ViewPort = &my_view_port;
  89.   /* The View should be interlaced: */
  90.   my_view.Modes = LACE;
  91.  
  92.  
  93.   /* 2. Prepare the ViewPort structure, and set some important values: */
  94.   InitVPort( &my_view_port );
  95.   my_view_port.DWidth = WIDTH;         /* Set the width.                */
  96.   my_view_port.DHeight = HEIGHT;       /* Set the height.               */
  97.   my_view_port.RasInfo = &my_ras_info; /* Give it a pointer to RasInfo. */
  98.   my_view_port.Modes = HIRES|LACE;     /* High resolution interlace.    */
  99.  
  100.  
  101.   /* IMPORTANT! If you want a ViewPort to be interlaced you have to set */
  102.   /* the LACE flag in both the ViewPort as well as in the View          */
  103.   /* structure. If the ViewPort is interlaced but the View is non-      */
  104.   /* interlaced, only every second line in the ViewPort would be drawn. */
  105.   /* If the ViewPort is non-interlaced but the View is interlaced,      */
  106.   /* each line in the ViewPort would be drawn twice.                    */
  107.  
  108.  
  109.   /* 3. Get a colour map, link it to the ViewPort, and prepare it: */
  110.   my_view_port.ColorMap = (struct ColorMap *) GetColorMap( COLOURS );
  111.   if( my_view_port.ColorMap == NULL )
  112.     clean_up( "Could NOT get a ColorMap!" );
  113.  
  114.   /* Get a pointer to the colour map: */
  115.   pointer = (UWORD *) my_view_port.ColorMap->ColorTable;
  116.  
  117.   /* Set the colours: */
  118.   for( loop = 0; loop < COLOURS; loop++ )
  119.     *pointer++ = my_color_table[ loop ];
  120.  
  121.  
  122.   /* 4. Prepare the BitMap: */
  123.   InitBitMap( &my_bit_map, DEPTH, WIDTH, HEIGHT );
  124.  
  125.   /* Allocate memory for the Raster: */ 
  126.   for( loop = 0; loop < DEPTH; loop++ )
  127.   {
  128.     my_bit_map.Planes[ loop ] = (PLANEPTR) AllocRaster( WIDTH, HEIGHT );
  129.     if( my_bit_map.Planes[ loop ] == NULL )
  130.       clean_up( "Could NOT allocate enough memory for the raster!" );
  131.  
  132.     /* Clear the display memory with help of the Blitter: */
  133.     BltClear( my_bit_map.Planes[ loop ], RASSIZE( WIDTH, HEIGHT ), 0 );
  134.   }
  135.  
  136.   
  137.   /* 5. Prepare the RasInfo structure: */
  138.   my_ras_info.BitMap = &my_bit_map; /* Pointer to the BitMap structure.  */
  139.   my_ras_info.RxOffset = 0;         /* The top left corner of the Raster */
  140.   my_ras_info.RyOffset = 0;         /* should be at the top left corner  */
  141.                                     /* of the display.                   */
  142.   my_ras_info.Next = NULL;          /* Single playfield - only one       */
  143.                                     /* RasInfo structure is necessary.   */
  144.  
  145.   /* 6. Create the display: */
  146.   MakeVPort( &my_view, &my_view_port );
  147.   MrgCop( &my_view );
  148.  
  149.  
  150.   /* 7. Prepare the RastPort, and give it a pointer to the BitMap. */
  151.   InitRastPort( &my_rast_port );
  152.   my_rast_port.BitMap = &my_bit_map;
  153.   
  154.  
  155.   /* 8. Show the new View: */
  156.   LoadView( &my_view );
  157.  
  158.  
  159.   /* Set the draw mode to JAM1. FgPen's colour will be used. */
  160.   SetDrMd( &my_rast_port, JAM1 );
  161.  
  162.   /* Draw 10000 lines in eight different colours, randomly. */ 
  163.   /* Position the pen: */
  164.   Move( &my_rast_port, rand() % WIDTH, rand() % HEIGHT );
  165.   for( loop = 0; loop < 10000; loop++ )
  166.   {
  167.     /* Set FgPen's colour (0-7): */
  168.     SetAPen( &my_rast_port, rand() % COLOURS );
  169.     /* Draw a line: */
  170.     Draw( &my_rast_port, rand() % WIDTH, rand() % HEIGHT );
  171.   }
  172.  
  173.  
  174.   /* 9. Restore the old View: */
  175.   LoadView( my_old_view );
  176.  
  177.  
  178.   /* Free all allocated resources and leave. */
  179.   clean_up( "THE END" );
  180. }
  181.  
  182.  
  183. /* Returns all allocated resources: */
  184. void clean_up( message )
  185. STRPTR message;
  186. {
  187.   int loop;
  188.  
  189.   /* Free automatically allocated display structures: */
  190.   FreeVPortCopLists( &my_view_port );
  191.   FreeCprList( my_view.LOFCprList );
  192.   FreeCprList( my_view.SHFCprList ); /* ! */
  193.  
  194.   /* An interlaced display use two copper lists (the normal LOF plus   */
  195.   /* the special SHF). When your program closes an interlaced ViewPort */
  196.   /* you must therefore deallocate both lists!                         */
  197.  
  198.  
  199.   /* Deallocate the display memory, BitPlane for BitPlane: */
  200.   for( loop = 0; loop < DEPTH; loop++ )
  201.     if( my_bit_map.Planes[ loop ] )
  202.       FreeRaster( my_bit_map.Planes[ loop ], WIDTH, HEIGHT );
  203.  
  204.   /* Deallocate the ColorMap: */
  205.   if( my_view_port.ColorMap ) FreeColorMap( my_view_port.ColorMap );
  206.  
  207.   /* Close the Graphics library: */
  208.   if( GfxBase ) CloseLibrary( GfxBase );
  209.  
  210.   /* Close the Intuition library: */
  211.   if( IntuitionBase ) CloseLibrary( IntuitionBase );
  212.  
  213.   /* Print the message and leave: */
  214.   printf( "%s\n", message ); 
  215.   exit();
  216. }
  217.